import json
import os
import openpyxl
import requests
import yaml
from common.Loger_log import error_loging
from common.Loger_log import write_loging

def get_dir():
    """ 获取项目根路径 """
    return os.path.split(os.path.split(os.path.realpath(__file__))[0])[0]

def read_config_file(one,two):
    with open(get_dir()+'/conftest.yml') as file:
        ll=yaml.load(stream=file,Loader=yaml.FullLoader)
        return ll[one][two]


def read_data_yml(data,url='/data/case_data.yml'):
    try:
        with open(get_dir()+url) as file:
            ll=yaml.load(stream=file, encoding='utf-8',Loader=yaml.FullLoader,)
            write_loging(f"读取yml文件成功,数据如下：{ll[data]}")
            return ll[data]
    except Exception as eror:
        error_loging(f"请输入用例文件地址在执行用例%s"%str(eror))

def read_data_excel(sheet,url='/data/login_data.xlsx'):
    try:
        wb = openpyxl.load_workbook(get_dir()+url)
        sheet = wb[sheet]
        data = []
        for row in sheet.iter_rows(values_only=True):
             data.append(row)
        write_loging(f"读取excel文件成功,数据如下：{data}")
        return data
    except Exception as eror:
        error_loging(f"请输入excel文件地址在执行用例%s"%str(eror))


def  send_message_to_feishu(case_zongshu,case_passe,case_fail,case_tiaoguo,case_eero,case_success,case_time):
    url='https://open.feishu.cn/open-apis/bot/v2/hook/7098c742-0bf3-4e54-8b4b-06805498a9e9'
    try:
        write_loging(f'发送消息地址：{url}')
        headers = {
            "Content-Type": "application/json"}
        message_body = {
            "msg_type": "post",
            "content": {
                "post": {
                    "zh_cn": {
                        "title": "自动化测试报告",
                        "content": [
                            [{
                                "tag": "text",
                                "text": f"执行测试总数: {case_zongshu} \n"
                                        f"通过测试数: {case_passe}\n"
                                        f"失败测试数: {case_fail}\n"
                                        f"测数跳过数: {case_tiaoguo}\n"
                                        f"执行错误数: {case_eero}\n"
                                        f"成功率: {case_success}\n"
                                        f"测试执行时间: {case_time}\n"
                                        f"测试报告点击查看详情:"
                            },
                                {
                                    "tag": "a",
                                    "text": "点击查看测试报告",
                                    "href": "http://127.0.0.1:8080/index.html"
                                }
                            ]
                        ]
                    }
                }
            }
        }
        res=requests.post(url,headers=headers,data=json.dumps(message_body))
        print(res.text)
    except Exception as eror:
        error_loging(f"发送消息失败，原因：{eror}")


if __name__ == '__main__':
     # print(read_config_file('base','dic_8013'))
      print(read_data_yml('login'))
     # print(read_data_excel('login'))
     # send_message_to_feishu(1,2,3,4,5,6,7)



